﻿using System;
using System.ServiceModel;

namespace YasharEl.Infrastructure.ServiceModel
{
    /// <summary>
    /// Generic service proxy wrapper.
    /// Clean up and close channel after using.
    /// </summary>
    /// <example>
    /// string response = null;
    /// ServiceProxy&lt;IUnitTestService&gt;.Call(p =>
    /// {
    ///     response = p.DoStuff();
    /// }
    /// </example>
    /// <example>
    /// string response = null;
    /// using(ServiceProxy&lt;IUnitTestService&gt; service = new ServiceProxy&lt;IUnitTestService&gt;())
    /// {
    ///     response = service.Proxy.DoStuff();
    /// }
    /// </example>
    /// <typeparam name="TInterface">The type of the service interface - service contract.</typeparam>
    public class ServiceProxy<TInterface> : ClientBase<TInterface>, IDisposable where TInterface : class
    {
        #region Constructors

        public ServiceProxy()
            : base(typeof(TInterface).ToString())
        {
        }

        public ServiceProxy(string endpointConfigurationName)
            : base(endpointConfigurationName)
        {
        }

        #endregion

        #region Proxy Property

        public TInterface Proxy
        {
            get
            {
                return this.Channel;
            }
        }

        #endregion

        #region Static Call Methods

        public static void Call(CallServiceDelegate<TInterface> proxyDelegate)
        {
            Call(proxyDelegate, typeof(TInterface).ToString());
        }

        public static void Call(CallServiceDelegate<TInterface> proxyDelegate, string endpointConfigurationName)
        {
            ChannelFactory<TInterface> channel = new ChannelFactory<TInterface>(endpointConfigurationName);
            try
            {
                proxyDelegate(channel.CreateChannel());
            }
            finally
            {
                if (channel.State == CommunicationState.Faulted)
                {
                    channel.Abort();
                }
                else
                {
                    try
                    {
                        channel.Close();
                    }
                    catch
                    {
                        channel.Abort();
                    }
                }
            }
        }

        #endregion

        #region IDisposable Implementations

        public void Dispose()
        {
            if (this.State == CommunicationState.Faulted)
            {
                base.Abort();
            }
            else
            {
                try
                {
                    base.Close();
                }
                catch
                {
                    base.Abort();
                }
            }
        }

        #endregion
    }

    #region Delegates

    public delegate void CallServiceDelegate<TInterface>(TInterface proxy) where TInterface : class;

    #endregion
}
